00001 /* 00002 * Copyright (c) 2013 Battelle Memorial Institute 00003 * Licensed under modified BSD License. A copy of this license can be found 00004 * in the LICENSE file in the top level directory of this distribution. 00005 */ 00006 // Emacs Mode Line: -*- Mode:c++;-*- 00007 // ------------------------------------------------------------- 00008 /** 00009 * @file configurable.h 00010 * @author William A. Perkins 00011 * @date 2016-12-08 14:06:45 d3g096 00012 * 00013 * @brief 00014 * 00015 * 00016 */ 00017 // ------------------------------------------------------------- 00018 00019 // SCCS ID: $Id$ Battelle PNL 00020 00021 #ifndef _configurable_h_ 00022 #define _configurable_h_ 00023 00024 #include <string> 00025 #include <boost/shared_ptr.hpp> 00026 #include "gridpack/configuration/configuration.hpp" 00027 00028 namespace gridpack { 00029 namespace utility { 00030 00031 // ------------------------------------------------------------- 00032 // class ConfigurableInterface 00033 // ------------------------------------------------------------- 00034 class ConfigurableInterface { 00035 public: 00036 00037 /// Copy constructor 00038 ConfigurableInterface(const ConfigurableInterface& old) {}; 00039 00040 /// Destructor 00041 virtual ~ConfigurableInterface(void) {}; 00042 00043 /// Get this instance's configuration key 00044 virtual std::string configurationKey(void) const = 0; 00045 00046 /// Set this instance's configuration key 00047 virtual void configurationKey(const std::string& s) = 0; 00048 00049 /// Is this instance configured? 00050 virtual bool isConfigured(void) const = 0; 00051 00052 /// Initialize this instance using the specified configuration property tree 00053 virtual void configure(utility::Configuration::CursorPtr theprops) = 0; 00054 00055 protected: 00056 00057 /// Default constructor (only for children) 00058 ConfigurableInterface(void) {}; 00059 00060 }; 00061 00062 00063 // ------------------------------------------------------------- 00064 // class Configurable 00065 // ------------------------------------------------------------- 00066 /** 00067 * This serves as the base for any class that has run-time options 00068 * that are read from the globally available Configuration 00069 * database. An instance of this class is assumed to have a uniquely 00070 * key which identifies its part of the Configuration option tree. 00071 * 00072 */ 00073 class Configurable 00074 : public ConfigurableInterface 00075 { 00076 public: 00077 00078 /// Copy constructor 00079 Configurable(const Configurable& old) 00080 : p_configCursor(old.p_configCursor), 00081 p_key(old.p_key), 00082 p_isConfigured(false) 00083 {} 00084 00085 /// Destructor 00086 ~Configurable(void) {} 00087 00088 /// Get this instance's configuration key 00089 std::string configurationKey(void) const 00090 { 00091 return p_key; 00092 } 00093 00094 /// Set this instance's configuration key 00095 void configurationKey(const std::string& s) 00096 { 00097 p_key = s; 00098 } 00099 00100 /// Is this instance configured? 00101 bool isConfigured(void) const 00102 { 00103 return p_isConfigured; 00104 } 00105 00106 /// Initialize this instance using the specified configuration property tree 00107 void configure(utility::Configuration::CursorPtr theprops) 00108 { 00109 if (theprops) { 00110 p_configCursor = theprops->getCursor(this->p_key); 00111 } 00112 this->p_configure(p_configCursor); 00113 p_isConfigured = true; 00114 } 00115 00116 protected: 00117 00118 /// Default constructor (only for children) 00119 Configurable(void) 00120 : p_configCursor(), 00121 p_key("bogus"), 00122 p_isConfigured(false) 00123 {} 00124 00125 /// Construct with a specified path (only for children) 00126 Configurable(const std::string& key) 00127 : p_configCursor(), 00128 p_key(key), 00129 p_isConfigured(false) 00130 {} 00131 00132 /// Specialized way to configure from property tree 00133 virtual void p_configure(utility::Configuration::CursorPtr props) = 0; 00134 00135 /// The configuration cursor used by this instance 00136 /** 00137 * The cursor given to configure() is saved in case it needs to be 00138 * referred to later. 00139 * 00140 */ 00141 utility::Configuration::CursorPtr p_configCursor; 00142 00143 private: 00144 00145 /// The configuration key 00146 std::string p_key; 00147 00148 /// Has this instance been configured 00149 bool p_isConfigured; 00150 00151 }; 00152 00153 // ------------------------------------------------------------- 00154 // class WrappedConfigurable 00155 // ------------------------------------------------------------- 00156 class WrappedConfigurable 00157 : public ConfigurableInterface 00158 { 00159 public: 00160 00161 /// Copy constructor 00162 WrappedConfigurable(const WrappedConfigurable& old) 00163 : ConfigurableInterface(old), 00164 p_configurable(old.p_configurable) 00165 {} 00166 00167 /// Destructor 00168 ~WrappedConfigurable(void) 00169 {} 00170 00171 /// Get this instance's configuration key 00172 std::string configurationKey(void) const 00173 { 00174 return p_configurable->configurationKey(); 00175 } 00176 00177 /// Set this instance's configuration key 00178 void configurationKey(const std::string& s) 00179 { 00180 p_configurable->configurationKey(s); 00181 } 00182 00183 /// Is this instance configured? 00184 bool isConfigured(void) const 00185 { 00186 return p_configurable->isConfigured(); 00187 } 00188 00189 /// Initialize this instance using the specified configuration property tree 00190 void configure(utility::Configuration::CursorPtr theprops) 00191 { 00192 this->p_preconfigure(theprops); 00193 p_configurable->configure(theprops); 00194 } 00195 00196 protected: 00197 00198 /// Default constructor (only for childred) 00199 WrappedConfigurable(void) 00200 : ConfigurableInterface(), p_configurable() 00201 {} 00202 00203 /// Constructor with existing Configurable instance (only for childred) 00204 WrappedConfigurable(Configurable *c) 00205 : ConfigurableInterface(), p_configurable(c) 00206 {} 00207 00208 /// The wrapped Configurable instance 00209 Configurable *p_configurable; 00210 00211 /// Set the wrapped Configurable instance 00212 void p_setConfigurable(Configurable *c) 00213 { 00214 p_configurable = c; 00215 } 00216 00217 /// Do something before configuring the configurable 00218 virtual void p_preconfigure(utility::Configuration::CursorPtr theprops) 00219 { 00220 // do nothing 00221 } 00222 00223 }; 00224 00225 00226 00227 } // namespace utility 00228 } // namespace gridpack 00229 00230 00231 00232 00233 #endif